package view.controller;

import common.Group;
import common.Message;
import common.NowUser;
import common.User;
import dao.CreatGroup;
import dao.UpdateGroup;
import javafx.event.ActionEvent;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import server.ClientConnServer;
import tools.GetId;
import view.CreateGroupChat;
import view.GroupChat;

import javax.swing.*;
import java.io.*;
import java.util.concurrent.BrokenBarrierException;

public class CreateGroupController {
    public TextField groupname;
    public TextArea groupintroduction;
    public Button creategroup;
    public Button groupback;
    public ImageView selfheadimg;
    public File localFile;

    public void togroupback(ActionEvent actionEvent) {
        Stage stage = (Stage) groupback.getScene().getWindow();
        stage.close();
    }

    public void creatgroupac(ActionEvent actionEvent) throws IOException, BrokenBarrierException, InterruptedException {
//        if(groupname.getText()==null || groupname.getText().equals("")){
//            JOptionPane.showMessageDialog(null, "名字不能为空", "提示", JOptionPane.ERROR_MESSAGE);
//        } else {
//            User user = new User();
//            user.setId(NowUser.getUser().getId());
//            String groupid=new GetId().getId();
//            Group group = new Group();
//            group.setGroupid(groupid);
//            group.setGroupname(groupname.getText());
//            group.setGroupstate(2);
//            group.setuserid(NowUser.user.getId());
//            if(groupintroduction.getText() == null || groupintroduction.getText().equals("")){
//                group.setGroupsign(groupintroduction.getPromptText());
//            }else{
//                group.setGroupsign(groupintroduction.getText());
//            }
//            Message msg = new Message();
//            msg.setGroup(group);
//            msg.setHeadimage(localFile);
//            if(msg.getHeadimage()!=null) {
//                //目标路径
//                String filePath = "E:/untitled/src/view/photo/";
//                File upfile = new File(filePath);
//                //如果文件目录不存在，就执行创建
//                if (!upfile.isDirectory()) {
//                    upfile.mkdirs();
//                }
//                File localFile = msg.getHeadimage();
//                String filename = new GetId().getId();
//                //目标文件名称
//                String targetName = filename + ".jpg";
//                System.out.println(targetName);
//                //创建目标文件
//                File targetFile = new File(filePath + targetName);
//                FileOutputStream fos = new FileOutputStream(targetFile);
//                //Common.user.setPhoto("C:/Users/86183/IdeaProjects/untitled2/src/view/image/"+targetName);
//                //获取本地文件输入流
//                InputStream stream = new FileInputStream(localFile);
//                //写入目标文件，字节流
//                byte[] buffer = new byte[1024 * 1024];
//                int byteRead = 0;//得到实际读取到的字节数 读到最后返回-1
//                while ((byteRead = stream.read(buffer)) != -1) {//把stream里的东西读到bytes数组里去
//                    //把字节转成String 从0到N变成String
//                    fos.write(buffer, 0, byteRead);
//                    fos.flush();
//                }
//                //一定要关闭文件流。
//                fos.close();
//                stream.close();
//                System.out.println("关闭上传群头像的文件流");
//                group.setGroupheadimage(filePath+targetName);
//            }else{
//                group.setGroupheadimage("");
//            }
//            msg.setGroup(group);
//            boolean flag = new UpdateGroup().creatGroup(msg.getGroup());
//            if(flag){
//                JOptionPane.showMessageDialog(null, "创建成功！", "提示", JOptionPane.PLAIN_MESSAGE);
//                NowUser.group=group;
//                Stage stage = (Stage) creategroup.getScene().getWindow();
//                stage.close();
//                Stage newstage = new Stage();
//                GroupChat rj = new GroupChat();
//                rj.start(newstage);
//            }else {
//                JOptionPane.showMessageDialog(null, "创建失败", "Title", JOptionPane.ERROR_MESSAGE);
//            }
//        }
        Message groupmsg = new Message();
        User user = new User();
        //user = Common.user;
        user.setId(NowUser.getUser().getId());
        String groupid=new GetId().getId();
        Group group = new Group();
        group.setGroupid(groupid);

        //System.out.println(user.getGroupid());
        group.setGroupname(groupname.getText());
        if(groupintroduction == null || groupintroduction.getText().equals("")){
            group.setGroupsign(groupintroduction.getPromptText());
        }else {
            group.setGroupsign(groupintroduction.getText());
        }
        group.setGroupstate(2);
        group.setuserid(NowUser.user.getId());
        groupmsg.setType("creatgroup");
        groupmsg.setGroup(group);
        groupmsg.setHeadimage(localFile);
        NowUser.creatgroup=false;
        new ClientConnServer().sendinfo(groupmsg);
        NowUser.cyclicBarrier.reset();
        NowUser.cyclicBarrier.await();
        System.out.println("创建的群聊id:" + groupmsg.getGroup().getGroupid());
        if(groupname.getText()==null || groupname.getText().equals("")){
            JOptionPane.showMessageDialog(null, "名字不能为空", "提示", JOptionPane.ERROR_MESSAGE);
        } else if(NowUser.creatgroup) {
            NowUser.group=group;
            Stage stage = (Stage) creategroup.getScene().getWindow();
            stage.close();
            Stage newstage = new Stage();
            GroupChat rj = new GroupChat();
            rj.start(newstage);
        } else {
            JOptionPane.showMessageDialog(null, "创建失败", "提示", JOptionPane.ERROR_MESSAGE);
        }
    }

    //上传头像
    public void upgroupimage(MouseEvent mouseEvent) {
        User user = new User();
        FileChooser fileChooser = new FileChooser(); //文件选择窗口
        fileChooser.setTitle("Open Resource File");  //窗口名
        fileChooser.setInitialDirectory(new File("C:\\Users")); //设置默认打开的文件路径
        File file = fileChooser.showOpenDialog(new Stage());//显示屏幕选择窗口
        // String filePath = file.getCanonicalPath()+"/fileSource"; //指定路径
        String str =null;
        if(file != null) {
            str = String.valueOf(file.getAbsolutePath());  // 方法返回选择文件的相对路径
            String url = "file:" + str;
            //Common.user.setGroupphoto(url);
            selfheadimg.setImage(new Image(url,100,100,true,true,true));
        }
        //读取本地文件
        localFile = new File(str);
    }
}
